import {defineStore} from "pinia";
// import {requestPost} from "@/api";
import router from "@/router";
import {renderIcon} from "@/utils";
import {QuestionCircle28Filled} from "@vicons/fluent";
import {StorageEnum} from '@/enums/storageEnum';
import {requestPost} from "../api";
import {Constants} from "../enums/constants.ts";

interface UserState {
    name: string;
    username: string;
    token: string;
    loginLoading: boolean;
}

export const useUserStore = defineStore("user-store", {
    state: (): UserState => ({
        name: "",
        username: "",
        token: "",
        loginLoading: false,
    }),
    getters: {
        /** 是否登录 */
        isLogin(state) {
            return Boolean(state.token);
        },
    },
    actions: {
        /**
         * 登录
         * @param username - 用户名
         * @param password - 密码
         */
        async login(username: string, password: string) {
            this.loginLoading = true;
            // 访问后端进行身份验证
            requestPost("/api/login", {
                username,
                password,
            }).then((res: any) => {
                if (res.code == 200) {
                    window.$message?.success(res.message);
                    sessionStorage.setItem(Constants.STORAGE_KEY_TOKEN, res.data.id);
                    sessionStorage.setItem(Constants.STORAGE_KEY_USERNAME, res.data.username);
                    sessionStorage.setItem(Constants.STORAGE_KEY_NAME, res.data.name);
                    sessionStorage.setItem(StorageEnum.IS_AUTHENTICATED, "TRUE");
                    // this.name = res.data.name;
                    // this.username = res.data.username;
                    // this.token = res.data.token;
                    // router.push("/");
                    setTimeout(() => {
                      router.push("/");
                    }, 1000);
                } else {
                    console.log(res.message)
                  window.$message?.error(res.message);
                }
            }).catch(err => {
                console.log(err)
                window.$message?.error(err.message)
            })
            this.loginLoading = false;
        },
        async logout() {
            window.$dialog?.warning({
                title: "系统提示",
                content: "确定要退出登录吗?",
                negativeText: '取消',
                positiveText: "确定",
                // closable: false,
                closeOnEsc: true,
                icon: renderIcon(QuestionCircle28Filled),
                onPositiveClick: () => {
                    // requestPost("/api/login").then((res: any) => {
                    //     if (res.code == 200) {
                    //         sessionStorage.clear()
                    //         window.$message?.success("已退出，转向登录页")
                    //         router.push("/login")
                    //     }
                    // })

                    sessionStorage.clear()
                    window.$message?.success("已退出，转向登录页")
                    router.push("/login")
                }
            })
        }
    },
});
